<?php
/**
 * RPCTask.php
 *
 * @package reepu.com
 * @version {$Id$}
 * @copyright 2009-2018 Shanghai Reepu Information Tech. Co., Ltd.
 * @license Commercial
 * @author Boolean since.
 * @datetime 2018-12-05 17:44
 * =================================================================
 * 版权所有 (C) 2009-2018 Reepu.com，并保留所有权利。
 * 网站地址:http://www.reepu.com/
 * -----------------------------------------------------------------
 * 您只能在不用于商业目的的前提下对程序代码进行修改和使用；
 * 不允许对程序代码以任何形式任何目的的再发布。
 * =================================================================
 */

namespace Reepu\Phalcon\Swoole;

use Hprose;
use Reepu;
class Service extends Server
{
    protected $explorer = null;

    protected $service_namespace = null;
    protected $service_directory = null;
    protected $service_suffix = null;

    public function onConstruct()
    {
        $this->usage = new Reepu\Swoole\Phalcon\Usage();
        $this->usage->check(['swoole', 'hprose', 'phalcon']);

        // 服务发现
        if (empty($this->discovery->toArray())) {
            throw new \Exception ("Must Need Config Provider" . PHP_EOL);
        }
        // 服务配置
        if (empty($this->rpc->service->toArray())) {
            throw new \Exception ("Must Need Config Service" . PHP_EOL);
        }

        // 使用Hprose Swoole
        $this->server = new Hprose\Swoole\Server($this->service->getListenUri());
        $this->server->setErrorTypes(E_ALL);
        $this->server->setDebugEnabled();
        $this->server->setCrossDomainEnabled();
        $this->server->set($this->swoole->toArray());

        $this->explorer = new Reepu\Service\Explorer();
        if (empty($this->rpc->service->namespace) && empty($this->service_namespace)) {
            throw new \Exception ("Must Need Config service namespace" . PHP_EOL);
        } elseif (!empty($this->rpc->service->namespace)) {
            $this->explorer->setNameSpace($this->rpc->service->namespace);
        }elseif(!empty($this->service_namespace)) {
            $this->explorer->setNameSpace($this->service_namespace);
        }

        if (empty($this->rpc->service->directory) && empty($this->service_directory)) {
            throw new \Exception ("Must Need Config service directory" . PHP_EOL);
        } elseif (!empty($this->rpc->service->directory)) {
            $this->explorer->setDirectory($this->rpc->service->directory);
        }elseif(!empty($this->service_directory)) {
            $this->explorer->setDirectory($this->service_directory);
        }

        if (empty($this->rpc->service->suffix) && empty($this->service_suffix)) {
            throw new \Exception ("Must Need Config service suffix" . PHP_EOL);
        } elseif (!empty($this->rpc->service->suffix)) {
            $this->explorer->setSuffix($this->rpc->service->suffix);
        } elseif (!empty($this->service_suffix)) {
            $this->explorer->setSuffix($this->service_suffix);
        }

        // 打印服务信息
        $this->printInfo();
    }

    public function startAction()
    {
        try {
            // 将扫描到有的服务类对外发布
            $this->addInstanceMethods();

            echo "Service Server Start Success" . PHP_EOL;
            $this->server->start(); // 启动服务
        } catch (\Exception $e) {
            echo $e->getMessage();
        }
    }

    /**
     * 将扫描到有的服务类对外发布
     */
    protected function addInstanceMethods()
    {
        foreach ($this->explorer->scan() as $item) {
            $this->server->addInstanceMethods(new $item['name'](), null, $item['alias']);
        }

        echo "Service {$this->service->getListenUri()} Register Success" . PHP_EOL;
        $this->service->register(); // 注册服务
    }
}